package cn.dglydrpy.fundmanagement;

import com.github.tusharepro.core.TusharePro;
import com.github.tusharepro.core.TushareProService;
import com.github.tusharepro.core.bean.StockBasic;
import com.github.tusharepro.core.common.KeyValue;
import com.github.tusharepro.core.entity.StockBasicEntity;
import com.github.tusharepro.core.http.Request;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableScheduling
@MapperScan("cn.dglydrpy.fundmanagement.dao.mapper")
public class FundManagementApplication {
    public static void main(String[] args) {
        SpringApplication.run(FundManagementApplication.class, args);

//        try {
//            test();
//        } catch (IOException e) {
//            System.err.println("调用Tushare API时发生错误: " + e.getMessage());
//            e.printStackTrace();
//        }
    }


    private static void test() throws IOException {

        final TusharePro.Builder builder = new TusharePro.Builder()
                .setToken("69bd47a33fad035302e78993c1b857457bd5cdabd3834248a3751856");  // 你的token

        TusharePro.setGlobal(builder.build());  // 设置全局配置

//        TushareProService.moneyflowHsgt(new Request<MoneyflowHsgtEntity>() {}
//            .allFields()
//            .param("trade_date", "20200210"))
//            .forEach(System.out::println);

//        TushareProService.ycCb(new Request<YcCbEntity>() {}
//                .param("ts_code", "1001.CB")
//                .param("curve_type", 1)
//                .param("trade_date", "20200203")
//                .allFields())
//                .forEach(System.out::println);

//        TushareProService.fundManager(new Request<FundManagerEntity>() {}
//            .allFields()
//            .param("ts_code", "150018.SZ"))
//            .forEach(System.out::println);

        final KeyValue<String, String> list_status = StockBasic.Params.list_status.value("L");

        // 打印 上海交易所所有上市的沪股通股票 信息
//        try {
//            TushareProService.stockBasic(new Request<StockBasicEntity>() {
//                    }  // 使用全局配置
//                            .allFields()  // 所有字段
//                            .param(StockBasic.Params.exchange.value("SSE"))  // 参数
//                            .param(list_status)  // 参数
//                            .param("is_hs", "H"))  // 参数
//                    .forEach(System.out::println);
//        } catch (Exception e) {
//            System.err.println("获取股票基础信息时发生错误: " + e.getMessage());
//            e.printStackTrace();
//        }

        final OkHttpClient defaultHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.NONE))
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();

        // 一个完整的例子
        try {
            TushareProService.stockBasic(
                            new Request<StockBasicEntity>(builder.copy()  // 将配置拷贝
                                    .setMaxRetries(5)  // 设置重试次数, 默认为3
                                    .setRetrySleepTimeUnit(TimeUnit.SECONDS)  // 设置重试sleep单位, 默认毫秒
                                    .setRetrySleepTimeOut(60L)  // 设置重试sleep时间, 默认为0
                                    .setRequestExecutor(Executors.newSingleThreadExecutor((r -> {
                                        Thread thread = new Thread(r);
                                        thread.setDaemon(true);
                                        return thread;
                                    })))  // 设置请求线程池, 默认CachedThreadPool
                                    .setHttpFunction(requestBytes -> {  // requestBytes -> function -> responseBytes
                                        try {
                                            okhttp3.Request request = new okhttp3.Request.Builder()
                                                    .url("http://api.tushare.pro")
                                                    .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBytes))
                                                    .build();

                                            return defaultHttpClient.newCall(request).execute().body().bytes();
                                        } catch (IOException e) {
                                            System.err.println("HTTP请求发生错误: " + e.getMessage());
                                            e.printStackTrace();
                                        }
                                        return null;
                                    })
                                    .build()) {
                            }
                                    .allFields()
                                    .param(StockBasic.Params.exchange.value("SZSE"))
                                    .param(list_status))
                    .stream()
                    .filter(x -> "银行".equals(x.getIndustry()))
                    .forEach(System.out::println);
        } catch (Exception e) {
            System.err.println("获取深圳交易所银行股信息时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}